home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Updates, etc. / PG PRO⁄PG Lite Demos / PG PRO Demo / PG PRO Demo.rsrc / TEXT_3008_STR#.INCL.txt < prev    next >
Text File  |  1993-09-10  |  19KB  |  408 lines

  1. '===============================================================================
  2. '=                     Copyright 1992 Staz‚Ñ¢ Software, Inc.                     =
  3. '=                             All rights reserved                             =
  4. '=                           "STR#.INCL" from PG:PRO                           =
  5. '===============================================================================
  6. INCLUDE FILE _aplIncl
  7. COMPILE 0,_MacsbugLabels_strResource_caseInsensitive'set by PG:PRO
  8. GLOBALS "PG PRO.GLBL"'include standard global file
  9. END GLOBALS
  10. INCLUDE "@Header.INCL"
  11. DEFSTR LONG
  12. '===============================================================================
  13. '  This set of functions has been designed to handle the work in manipulating
  14. '  information in STR# resources.  Since PG:PRO's list manager CDEF uses
  15. '  STR# resources to handle data for lists, the set is an important part of any
  16. '  application that uses lists.
  17. '
  18. '  FN delElement(theElem,strID) -- deletes any element of a STR# resource
  19. '
  20. '  FN insElement(theElem,strID,theTxt$) -- inserts the string specified by
  21. '     "theTxt$" before the element specified by "theElem" into a STR# resource
  22. '
  23. '  FN repElement(theElem,strID,theTxt$) -- replaces any element of a STR# res
  24. '
  25. '  FN apndElement(strID,theTxt$) -- adds an element to the end of a STR# res
  26. '
  27. '  FN sortStrRes(strID) -- sort a STR# resource... FAST!!
  28. '
  29. '  FN viewListItem(btnRefNum,itemToView) -- If you make a change to a STR# used
  30. '     in one of PG's scrolling lists or wish to scroll to an item that is not
  31. '     visible, call this function.
  32. '
  33. '  FN index2res(theIndx,strID) -- this function takes an INDEX$ array and
  34. '     converts it to a STR# resource that is saved in the current file.
  35. '
  36. '  FN res2Index(theIndx,strID) -- this function takes a STR# resource and
  37. '     converts it to an INDEX$ array.
  38. '
  39. '  FN newStr(strID,theText$) -- creates a new STR# resource with a single
  40. '     element.
  41. '
  42. '  FN countStr(strID) -- this function returns the number of elements in a
  43. '     STR# resource.
  44. '
  45. '  FN LMCDappend(btnRefNum,strID,theTxt$) -- Use this function to append
  46. '     a string to one of PG's list manager controls.
  47. '
  48. '  FN LMCDremove(btnRefNum,strID) -- use this to remove a single line
  49. '     from a scrolling list
  50. '
  51. '  FN LMCDfind(btnRefNum,strID,theTxt$) -- this function locates a
  52. '     string in a scrolling list and returns the element number for
  53. '     that string.  If the string is not found, the function returns
  54. '     zero.
  55. '
  56. '===============================================================================
  57. '_______________________________________________________________________________
  58. LOCAL FN chkResErr'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  59. '—————————————————————————————————————————————————————————————————————————————
  60. DIM t$(1)
  61. rError = FN RESERROR
  62. LONG IF rError
  63. t$(0)="CHANGERESOURCE failed."+CHR$(13)+"The modified resource could not be marked as changed."
  64. t$(1) = "STR# Error"
  65. CALL PARAMTEXT(t$(0),t$(1),"","")
  66. FN pGshowErr(0)
  67. END IF
  68. END FN = rError
  69. '_______________________________________________________________________________
  70. '_______________________________________________________________________________
  71. LOCAL FN delElement(theElem,theID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  72. '—————————————————————————————————————————————————————————————————————————————
  73. resHndl&   = FN GETRESOURCE(_"STR#",theID)
  74. boolError  = _noErr
  75. LONG IF resHndl&
  76. delLgth  = LEN(STR#(theID,theElem)) + 1
  77. LONG IF delLgth -1
  78. hState = FN HGETSTATE(resHndl&)
  79. OSErr  = FN HNOPURGE(resHndl&)
  80. dest&  = USR STROFFSET(theElem,theID)
  81. src&   = dest& + delLgth
  82. mvSz&  = FN GETHANDLESIZE(resHndl&) - src&
  83. BLOCKMOVE [resHndl&]+src&,[resHndl&] + dest&,mvSz&
  84. newSz& = FN GETHANDLESIZE(resHndl&) - delLgth
  85. OSErr  = FN SETHANDLESIZE(resHndl&,newSz&)
  86. % [resHndl&],{[resHndl&]}-1
  87. resHndl& = FN STRIPADDRESS(resHndl&)
  88. CALL CHANGEDRESOURCE(resHndl&)
  89. boolError = FN chkResErr
  90. OSErr  = FN HSETSTATE(resHndl&,hState)
  91. END IF
  92. END IF
  93. END FN = boolError
  94. '_______________________________________________________________________________
  95. LOCAL FN insElement(theElem,theID,theTxt$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  96. '—————————————————————————————————————————————————————————————————————————————
  97. resHndl&  = FN GETRESOURCE(_"STR#",theID)
  98. boolError = _noErr
  99. LONG IF resHndl&
  100. hState  = FN HGETSTATE(resHndl&)
  101. OSErr   = FN HNOPURGE(resHndl&)
  102. insLgth = LEN(theTxt$) + 1
  103. oldSz&  = FN GETHANDLESIZE(resHndl&)
  104. newSz&  = oldSz& + insLgth
  105. src&    = USR STROFFSET(theElem,theID)
  106. dest&   = src&   + insLgth
  107. mvSz&   = oldSz& - src&
  108.  
  109. LONG IF FN SETHANDLESIZE(resHndl&,newSz&) = 0
  110. BLOCKMOVE [resHndl&]+src&,[resHndl&] + dest&,mvSz&
  111. BLOCKMOVE @theTxt$,[resHndl&]+src&,insLgth
  112. % [resHndl&],{[resHndl&]}+1
  113. resHndl& = FN STRIPADDRESS(resHndl&)
  114. CALL CHANGEDRESOURCE(resHndl&)
  115. boolError = FN chkResErr
  116. END IF
  117.  
  118. OSErr   = FN HSETSTATE(resHndl&,hState)
  119. END IF
  120. END FN = boolError
  121. '_______________________________________________________________________________
  122. LOCAL FN repElement(theElem,theID,theTxt$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  123. '—————————————————————————————————————————————————————————————————————————————
  124. boolError = FN delElement(theElem,theID)
  125. LONG IF boolError = _noErr
  126. boolError = FN insElement(theElem,theID,theTxt$)
  127. END IF
  128. END FN = boolError
  129. '_______________________________________________________________________________
  130. LOCAL FN apndElement(theID,theTxt$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  131. '—————————————————————————————————————————————————————————————————————————————
  132. resHndl& = FN GETRESOURCE(_"STR#",theID)
  133. boolError = _noErr
  134. LONG IF resHndl&
  135. hState = FN HGETSTATE(resHndl&)
  136. OSErr  = FN HNOPURGE(resHndl&)
  137. DEF APNDSTR(theTxt$,resHndl&)
  138. resHndl& = FN STRIPADDRESS(resHndl&)
  139. CALL CHANGEDRESOURCE(resHndl&)
  140. boolError = FN chkResErr
  141. OSErr  = FN HSETSTATE(resHndl&,hState)
  142. END IF
  143. END FN = boolError
  144. '_______________________________________________________________________________
  145. LOCAL FN sortStrRes(theID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  146. '—————————————————————————————————————————————————————————————————————————————
  147. XREF @aryHndl&(32000)
  148. boolError = _zTrue
  149. resHndl&  = FN GETRESOURCE(_"STR#",theID)
  150.  
  151. LONG IF resHndl&
  152. hState  = FN HGETSTATE(resHndl&)
  153. OSErr   = FN HLOCK(resHndl&)
  154. elemCnt = {[resHndl&]}
  155. LONG IF elemCnt > 1
  156. aryHndl& = FN NEWHANDLE _clear((elemCnt+1)*4)
  157. ptr& = [resHndl&]+2
  158. FOR pstrLoop = 1 TO elemCnt
  159. aryHndl&(pstrLoop) = ptr&
  160. ptr& = ptr& + PEEK(ptr&) + 1
  161. NEXT pstrLoop
  162. '=================================================================
  163. gap = elemCnt
  164. DO
  165.     gap = gap/1.3
  166.     IF gap < 1 THEN gap = 1
  167.     switch = _false
  168.     FOR sortLoop = 1 TO elemCnt - gap
  169.         test = sortLoop + gap
  170.         LONG IF PSTR$(aryHndl&(sortLoop)) > PSTR$(aryHndl&(test))
  171.             SWAP aryHndl&(sortLoop),aryHndl&(test)
  172.             switch = _zTrue
  173.         END IF
  174.     NEXT sortLoop
  175. UNTIL switch =_false AND gap=1
  176. '=================================================================
  177. theSize& = FN GETHANDLESIZE(resHndl&)
  178. LONG IF theSize&
  179. newRes&  = FN NEWHANDLE(theSize&)
  180. LONG IF newRes&
  181. OSErr = FN HLOCK(newRes&)
  182. % [newRes&],elemCnt
  183. ptr& = [newRes&]+2
  184.  
  185. FOR refill = 1 TO elemCnt
  186. l = PEEK(aryHndl&(refill))+1
  187. BLOCKMOVE aryHndl&(refill),ptr&,l
  188. ptr& = ptr& + l
  189. NEXT
  190.  
  191. BLOCKMOVE [newRes&],[resHndl&],theSize&
  192. resHndl& = FN STRIPADDRESS(resHndl&)
  193. CALL CHANGEDRESOURCE(resHndl&)
  194. boolError = FN chkResErr
  195. DEF DISPOSEH(newRes&)
  196. END IF
  197.  
  198. DEF DISPOSEH(aryHndl&)
  199. END IF
  200. END IF
  201. OSErr  = FN HSETSTATE(resHndl&,hState)
  202. END IF
  203. END FN = boolError
  204. '_______________________________________________________________________________
  205. LOCAL FN viewListItem(btnRefNum,itemToView)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  206. '—————————————————————————————————————————————————————————————————————————————
  207. DIM localRect;0,t,l,b,r
  208. oldValue  = BUTTON(btnRefNum)'save old button value
  209. ctrlHndl& = BUTTON&(btnRefNum)'get control handle
  210. LONG IF ctrlHndl&
  211. multiCol  = {[[[ctrlHndl&]+_contrlData]]+36}'1 if multi col
  212. numRows   = {[[[ctrlHndl&]+_contrlData]]+40}'number of rows visible
  213. topItem   = {[[[ctrlHndl&]+_contrlData]]}'top item visible
  214. strID     = {[[[ctrlHndl&]+_contrlData]]+2}'ID of STR# resource
  215. resHndl&  = FN GETRESOURCE(_"STR#",strID)'handle to STR res
  216. LONG IF resHndl&'got a handle?
  217. strCount = {[resHndl&]}'extract element count
  218. % [ctrlHndl&]+_contrlmax,strCount-1'
  219. IF itemToView > strCount THEN itemToView = strCount
  220. IF itemToView < 1 + multiCol THEN itemToView = 1 + multiCol
  221.  
  222. END IF'check new value
  223.  
  224. viewAdj = 1 - multiCol
  225. LONG IF itemToView - viewAdj < topItem'above top item?
  226. % [[[ctrlHndl&]+_contrlData]],itemToView - viewAdj'reset top item
  227. END IF
  228.  
  229. LONG IF itemToView > topItem + numRows + 1'below lowest visible item?
  230. % [[[ctrlHndl&]+_contrlData]],itemToView - (numRows + 1)
  231. END IF'reset top to make it visible
  232.  
  233. LONG IF itemToView = oldValue'no change in value?
  234. localRect;8=[ctrlHndl&]+_contrlRect
  235. CALL INSETRECT(localRect,1,1)
  236. CALL INVALRECT(localRect)'invalidate to force redraw
  237. l=r+1:r=l+14
  238. CALL INVALRECT(localRect)'invalidate to force redraw
  239. XELSE'otherwise
  240. BUTTON btnRefNum,itemToView'have FB update it
  241. END IF
  242. END IF
  243. END FN
  244. '_______________________________________________________________________________
  245. LOCAL FN index2res(theIndx,resID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  246. '—————————————————————————————————————————————————————————————————————————————
  247. strHndl& = FN NEWHANDLE _clear(2)
  248. LONG IF strHndl&
  249. elemCount = MEM(10 + theIndx) - 1
  250. FOR loop = 0 TO elemCount
  251. t$ = INDEX$(loop,theIndx)
  252. DEF APNDSTR(t$,strHndl&)
  253. NEXT
  254. FN pGreplaceRes(strHndl&,_"STR#",resID,"")
  255. END IF
  256. END FN
  257. '_______________________________________________________________________________
  258. LOCAL FN res2Index(theIndx,resID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  259. '—————————————————————————————————————————————————————————————————————————————
  260. resHndl& = FN GETRESOURCE(_"STR#",resID)
  261. LONG IF resHndl&
  262. theSize& = FN GETHANDLESIZE(resHndl&) + 1024
  263. theCount = {[resHndl&]}
  264. CLEAR INDEX$ theIndx
  265. CLEAR theSize&, theIndx
  266. FOR loop = 1 TO theCount
  267. INDEX$(loop-1,theIndx) = STR#(resID,loop)
  268. NEXT
  269. END IF
  270. END FN
  271. '_______________________________________________________________________________
  272. LOCAL FN newStr(strID,theText$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  273. '—————————————————————————————————————————————————————————————————————————————
  274. resHndl& = FN NEWHANDLE _clear(2)
  275. DEF APNDSTR(theText$,resHndl&)
  276. FN pGreplaceRes(resHndl&,_"STR#",strID,"")
  277. END FN
  278. '_______________________________________________________________________________
  279. LOCAL FN countStr(strID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  280. '—————————————————————————————————————————————————————————————————————————————
  281. resHndl& = FN GETRESOURCE(_"STR#",strID)
  282. LONG IF resHndl&
  283. theCount = {[resHndl&]}
  284. XELSE
  285. theCount = 0
  286. END IF
  287. END FN = theCount
  288. '_______________________________________________________________________________
  289. DEF FN parseToComma(@srcStr&) USING "Parse To Comma"'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  290. '———————————————————————————————————————————————————————————————————————————————
  291. GOTO"After Parse To Comma"
  292. "Parse To Comma"
  293. '--------------------------------------------------------------------------
  294. '  This function searches for a comma and truncates the string
  295. '  to eliminate the comma and all following characters.
  296. '
  297. '      EXAMPLE     src$  = "one,two,three"
  298. '                  fn parseToComma(src$)
  299. '                  src$ is now equal to "one"
  300. '
  301. '       D0 Original string address
  302. '       D1 Character count
  303. '       D2 This character
  304. '       A0 Points to present string pos
  305. '--------------------------------------------------------------------------
  306. `             MOVE.L  D0,A0             ;address to A0
  307. `             MOVEQ   #0,D1             ;clear D1,D2
  308. `             MOVEQ   #0,D2
  309. `             MOVE.B  (A0)+,D1          ;get string length
  310. `             BEQ.S   parseDone         ;empty string
  311. `             SUBQ    #1,D1             ;decrement for DBRA
  312. `notYet       MOVE.B  (A0)+,D2          ;get next character
  313. `             CMPI.B  #',',D2           ;is it a comma?
  314. `             BEQ.S   foundComma        ;yep - exit
  315. `             DBRA    D1,notYet         ;nope - keep looking
  316. `             BRA.S   parseDone         ;never found it
  317. `foundComma   ADDQ    #1,D1             ;reverse the -1 used for DBRA
  318. `             MOVE.L  D0,A0             ;get original address
  319. `             SUB.B   D1,(A0)           ;use for length byte
  320. `parseDone    RTS                       ;done
  321. "After Parse To Comma"
  322. '_______________________________________________________________________________
  323. LOCAL FN parseFromComma$(@srcStr&)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  324. '—————————————————————————————————————————————————————————————————————————————
  325. '  This function searches for a comma and moves the following characters
  326. '  of the string into a target string.
  327. '
  328. '      EXAMPLE     src$  = "one,two,three"
  329. '                  dest$ = fn parseFromComma(src$)
  330. '                  dest$ is now equal to "two,three"
  331. '
  332. '       A0 Original string address (After move on entry)
  333. '       A1 Target String
  334. '       D0 Source string size
  335. '       D1 Dest string byte count
  336. '       D2 Character being checked
  337. '--------------------------------------------------------------------------
  338. `             MOVE.L  D0,A0             ;source address to A0
  339. `             LEA     ^t$,A1            ;target address in A1
  340. `             MOVE.B  #0,(A1)+          ;default - no text found
  341. `             MOVEQ   #0,D0             ;clear D0-D2
  342. `             MOVEQ   #0,D1
  343. `             MOVEQ   #0,D2
  344.  
  345. `             MOVE.B  (A0)+,D0          ;get string length
  346. `             BEQ.S   noComma           ;empty string
  347. `             SUBQ    #1,D0             ;decrement for DBRA
  348. `keepLokn     MOVE.B  (A0)+,D2          ;get next character
  349. `             CMPI.B  #',',D2           ;is it a comma?
  350. `             BEQ.S   gotComma          ;yep - exit
  351. `             DBRA    D0,keepLokn       ;nope - keep looking
  352. `             BRA.S   noComma           ;never found it
  353.  
  354. `gotComma     MOVE.B  (A0)+,(A1)+       ;first move is actually comma
  355. `             ADDQ    #1,D1             ;increment our string counter
  356. `             DBRA    D0,gotComma       ;go till done
  357. `             SUBQ    #1,D1             ;adjust - added in the comma byte
  358. `             LEA     ^t$,A1            ;load string address again
  359. `             MOVE.B  D1,(A1)           ;set length byte
  360.  
  361. `noComma
  362. '--------------------------------------------------------------------------
  363. END FN = t$
  364. '_______________________________________________________________________________
  365. LOCAL FN LMCDappend(btnID,resID,theTxt$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  366. '—————————————————————————————————————————————————————————————————————————————
  367. boolError = _noErr
  368. LONG IF STR#(resID,BUTTON(btnID)) = "Empty List"
  369. FN newStr(resID,theTxt$)
  370. XELSE
  371. FN apndElement(resID,theTxt$)
  372. END IF
  373. FN viewListItem(btnID,FN countStr(resID))
  374. END FN = boolError
  375. '_______________________________________________________________________________
  376. LOCAL FN LMCDremove(btnID,resID)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  377. '—————————————————————————————————————————————————————————————————————————————
  378. boolError = _noErr
  379. LONG IF STR#(resID,BUTTON(btnID)) = "Empty List"
  380. BEEP
  381. XELSE
  382. LONG IF FN countStr(resID) = 1
  383. FN newStr(resID,"Empty List")
  384. XELSE
  385. FN delElement(BUTTON(btnID),resID)
  386. END IF
  387. END IF
  388. LONG IF BUTTON(btnID) > FN countStr(resID)
  389. BUTTON btnID,FN countStr(resID)
  390. XELSE
  391. FN viewListItem(btnID,BUTTON(btnID))
  392. END IF
  393. END FN = boolError
  394. '_______________________________________________________________________________
  395. LOCAL FN LMCDfind(btnID,resID,theTxt$)'‚àë‚àë≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì≈ì‚àë‚àë
  396. '—————————————————————————————————————————————————————————————————————————————
  397. STRfound = _false
  398. theCount = FN countStr(resID)
  399. FOR loop = 1 TO theCount
  400. LONG IF theTxt$ = STR#(resID,loop)
  401. LONG IF BUTTON(btnID) <> loop
  402. FN viewListItem(btnID,loop)
  403. END IF
  404. STRfound = loop
  405. loop = theCount
  406. END IF
  407. NEXT
  408. END FN = STRfound